{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Game of Life"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #matrix #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #矩阵 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: gameOfLife"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #生命游戏"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>根据&nbsp;<a href=\"https://baike.baidu.com/item/%E7%94%9F%E5%91%BD%E6%B8%B8%E6%88%8F/2926434?fr=aladdin\" target=\"_blank\">百度百科</a>&nbsp;，&nbsp;<strong>生命游戏</strong>&nbsp;，简称为 <strong>生命</strong> ，是英国数学家约翰·何顿·康威在 1970 年发明的细胞自动机。</p>\n",
    "\n",
    "<p>给定一个包含 <code>m × n</code>&nbsp;个格子的面板，每一个格子都可以看成是一个细胞。每个细胞都具有一个初始状态： <code>1</code> 即为 <strong>活细胞</strong> （live），或 <code>0</code> 即为 <strong>死细胞</strong> （dead）。每个细胞与其八个相邻位置（水平，垂直，对角线）的细胞都遵循以下四条生存定律：</p>\n",
    "\n",
    "<ol>\n",
    "\t<li>如果活细胞周围八个位置的活细胞数少于两个，则该位置活细胞死亡；</li>\n",
    "\t<li>如果活细胞周围八个位置有两个或三个活细胞，则该位置活细胞仍然存活；</li>\n",
    "\t<li>如果活细胞周围八个位置有超过三个活细胞，则该位置活细胞死亡；</li>\n",
    "\t<li>如果死细胞周围正好有三个活细胞，则该位置死细胞复活；</li>\n",
    "</ol>\n",
    "\n",
    "<p>下一个状态是通过将上述规则同时应用于当前状态下的每个细胞所形成的，其中细胞的出生和死亡是同时发生的。给你 <code>m x n</code> 网格面板 <code>board</code> 的当前状态，返回下一个状态。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/12/26/grid1.jpg\" />\n",
    "<pre>\n",
    "<strong>输入：</strong>board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]\n",
    "<strong>输出：</strong>[[0,0,0],[1,0,1],[0,1,1],[0,1,0]]\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/12/26/grid2.jpg\" />\n",
    "<pre>\n",
    "<strong>输入：</strong>board = [[1,1],[1,0]]\n",
    "<strong>输出：</strong>[[1,1],[1,1]]\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>m == board.length</code></li>\n",
    "\t<li><code>n == board[i].length</code></li>\n",
    "\t<li><code>1 &lt;= m, n &lt;= 25</code></li>\n",
    "\t<li><code>board[i][j]</code> 为 <code>0</code> 或 <code>1</code></li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>进阶：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li>你可以使用原地算法解决本题吗？请注意，面板上所有格子需要同时被更新：你不能先更新某些格子，然后使用它们的更新后的值再更新其他格子。</li>\n",
    "\t<li>本题中，我们使用二维数组来表示面板。原则上，面板是无限的，但当活细胞侵占了面板边界时会造成问题。你将如何解决这些问题？</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [game-of-life](https://leetcode.cn/problems/game-of-life/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [game-of-life](https://leetcode.cn/problems/game-of-life/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[0,1,0],[0,0,1],[1,1,1],[0,0,0]]', '[[1,1],[1,0]]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def gameOfLife(self, board: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify board in-place instead.\n",
    "        \"\"\"\n",
    "        directions = [(1,0),(-1,0),(0,1),(0,-1),(-1,-1),(-1,1),(1,-1),(1,1)]\n",
    "        for i in range(len(board)):\n",
    "            for j in range(len(board[0])):\n",
    "                lives = 0\n",
    "                for di,dj in directions:\n",
    "                    if 0<=i+di<len(board) and 0<=j+dj<len(board[0]) and abs(board[i+di][j+dj]) == 1:\n",
    "                        lives += 1\n",
    "                if board[i][j] == 0 and lives == 3:\n",
    "                    board[i][j] = -2\n",
    "                if board[i][j] == 1 and (lives < 2 or lives > 3):\n",
    "                    board[i][j] = -1\n",
    "        for i in range(len(board)):\n",
    "            for j in range(len(board[0])):\n",
    "                if board[i][j] == -1:\n",
    "                    board[i][j] = 0\n",
    "                elif board[i][j] == -2:\n",
    "                    board[i][j] = 1\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def gameOfLife(self, board: 'List[List[int]]') -> 'None':\n",
    "        \"\"\"\n",
    "        Do not return anything, modify board in-place instead.\n",
    "        \"\"\"\n",
    "        row, col = len(board), len(board[0])\n",
    "        for i in range(row):\n",
    "            for j in range(col):\n",
    "                count = self.__serach(board, i, j, row - 1, col - 1)\n",
    "                # 如果当前位置是1，并且周围有两个1;或者当前周围有3个1\n",
    "                if (board[i][j] and count == 2) or count == 3:\n",
    "                    board[i][j] |= 0b10\n",
    "        for i in range(row):\n",
    "            for j in range(col):\n",
    "                board[i][j] >>= 1\n",
    "        return\n",
    "\n",
    "    def __serach(self, board, i, j, row, col):\n",
    "        # 统计当前位置的临近的8个方格内1的个数\n",
    "        count = 0\n",
    "        # 获取行的边界\n",
    "        row1, row2 = max(0, i - 1), min(row, i + 1)\n",
    "        # 获取列的边界\n",
    "        col1, col2 = max(0, j - 1), min(col, j + 1)\n",
    "        for k in range(row1, row2 + 1):\n",
    "            for m in range(col1, col2 + 1):\n",
    "                count += (board[k][m] & 1)\n",
    "        # 减去本身这个位置的值\n",
    "        return count - (board[i][j] & 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def gameOfLife(self, board):\n",
    "        \"\"\"\n",
    "        :type board: List[List[int]]\n",
    "        :rtype: void Do not return anything, modify board in-place instead.\n",
    "        \"\"\"\n",
    "        # if board[i][j]==0 and its value should be changed, set board[i][j]=2;\n",
    "        # if board[i][j]==1 and its value should be changed, set board[i][j]=3;\n",
    "        \n",
    "        row = len(board)\n",
    "        col = len(board[0])\n",
    "        for i in range(row):\n",
    "            for j in range(col):\n",
    "                n=0\n",
    "                if i-1>=0:\n",
    "                    n += board[i-1][j]%2\n",
    "                    if j-1 >=0:\n",
    "                        n += board[i-1][j-1]%2\n",
    "                    if j+1 <col:\n",
    "                        n += board[i-1][j+1]%2\n",
    "                if i+1<row:\n",
    "                    n += board[i+1][j]%2\n",
    "                    if j-1 >=0:\n",
    "                        n += board[i+1][j-1]%2\n",
    "                    if j+1 <col:\n",
    "                        n += board[i+1][j+1]%2\n",
    "                if j-1>=0:\n",
    "                    n += board[i][j-1]%2\n",
    "                if j+1<col:\n",
    "                    n += board[i][j+1]%2\n",
    "                \n",
    "\n",
    "                if board[i][j]==1:\n",
    "                    if n<2 or n>3:\n",
    "                        board[i][j]=3\n",
    "                elif board[i][j]==0:\n",
    "                    print(i,j,n)\n",
    "                    if n==3:\n",
    "                        board[i][j]=2\n",
    "                        \n",
    "                \n",
    "        #update\n",
    "        for i in range(row):\n",
    "            for j in range(col):\n",
    "               # print(board[i][j],end=\"  \")\n",
    "                if board[i][j]==2:\n",
    "                    board[i][j]=1\n",
    "                elif board[i][j]==3:\n",
    "                    board[i][j]=0\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def gameOfLife(self, board):\n",
    "        \"\"\"\n",
    "        :type board: List[List[int]]\n",
    "        :rtype: void Do not return anything, modify board in-place instead.\n",
    "        \"\"\"\n",
    "        h=len(board)\n",
    "        w=len(board[0])\n",
    "        for i in range(h):\n",
    "            for j in range(w):\n",
    "                if board[i][j]%10==1:\n",
    "                    if i>0:\n",
    "                        board[i-1][j]+=10\n",
    "                        if j>0:\n",
    "                            board[i-1][j-1]+=10\n",
    "                        if j<w-1:\n",
    "                            board[i-1][j+1]+=10\n",
    "                    if i<h-1:\n",
    "                        board[i+1][j]+=10\n",
    "                        if j>0:\n",
    "                            board[i+1][j-1]+=10\n",
    "                        if j<w-1:\n",
    "                            board[i+1][j+1]+=10\n",
    "                    if j>0:\n",
    "                        board[i][j-1]+=10\n",
    "                    if j<w-1:\n",
    "                        board[i][j+1]+=10\n",
    "        for i in range(h):\n",
    "            for j in range(w):\n",
    "                if board[i][j]==30 or board[i][j]==21 or board[i][j]==31:\n",
    "                    board[i][j]=1\n",
    "                else:\n",
    "                    board[i][j]=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def gameOfLife(self, board):\n",
    "        \"\"\"\n",
    "        :type board: List[List[int]]\n",
    "        :rtype: void Do not return anything, modify board in-place instead.\n",
    "        \"\"\"\n",
    "        list1=[]\n",
    "        for i in board:\n",
    "            list1.append(i.copy())\n",
    "        a=len(board)\n",
    "        b=len(board[0])\n",
    "        print(a,b,list1)\n",
    "        for i in range(a):\n",
    "            for j in range(b):\n",
    "                jishu=0\n",
    "                if i-1>-1 and j-1>-1 and list1[i-1][j-1]==1:\n",
    "                    jishu+=1\n",
    "                if i-1>-1 and list1[i-1][j]==1:\n",
    "                    jishu+=1\n",
    "                #print(i-1,j+1,b,list1,11111)\n",
    "                if i-1>-1 and j+1<b and list1[i-1][j+1]==1:\n",
    "                    jishu+=1;print(jishu,i,j,1)\n",
    "                if  j-1>-1 and list1[i][j-1]==1:\n",
    "                    jishu+=1\n",
    "                if  j+1<b and list1[i][j+1]==1:\n",
    "                    jishu+=1  \n",
    "                if  i+1<a and j-1>-1 and list1[i+1][j-1]==1:\n",
    "                    jishu+=1 \n",
    "                if  i+1<a and list1[i+1][j]==1:\n",
    "                    jishu+=1 ;print(jishu,i,j,2)\n",
    "                if  i+1<a and j+1<b and list1[i+1][j+1]==1:\n",
    "                    jishu+=1 ;print(jishu,i,j,3)\n",
    "                if jishu <2:board[i][j]=0\n",
    "                if jishu==3:board[i][j]=1\n",
    "                if jishu>3:board[i][j]=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def gameOfLife(self, board: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify board in-place instead.\n",
    "        \"\"\"\n",
    "        if not board or not board[0]:\n",
    "            return\n",
    "        fuzhu = [[0] * len(board[0]) for _ in range(len(board))]\n",
    "        if len(board[0]) > 1:\n",
    "            for i in range(len(board)):\n",
    "                fuzhu[i][0] = (board[i][0] == 1) + (board[i][1] == 1)\n",
    "                for j in range(1, len(board[0]) - 1):\n",
    "                    fuzhu[i][j] = (board[i][j - 1] == 1) + (board[i][j] == 1) + (board[i][j + 1] == 1)\n",
    "                fuzhu[i][-1] = (board[i][-1] == 1) + (board[i][-2] == 1)\n",
    "        if len(board) == 1:\n",
    "            for j in range(len(board[0])):\n",
    "                if board[0][j] == 1 and fuzhu[0][j] != 3:\n",
    "                        board[0][j] = 0\n",
    "        else:\n",
    "            for j in range(len(board[0])):\n",
    "                tmp = fuzhu[0][j] + fuzhu[1][j]\n",
    "                if board[0][j] == 0 and tmp == 3:\n",
    "                    board[0][j] = 1\n",
    "                elif board[0][j] == 1:\n",
    "                    if tmp < 3 or tmp > 4:\n",
    "                        board[0][j] = 0\n",
    "                for i in range(1, len(board) - 1):\n",
    "                    tmp = fuzhu[i - 1][j] + fuzhu[i][j] + fuzhu[i + 1][j]\n",
    "                    if board[i][j] == 0 and tmp == 3:\n",
    "                        board[i][j] = 1\n",
    "                    elif board[i][j] == 1:\n",
    "                        if tmp < 3 or tmp > 4:\n",
    "                            board[i][j] = 0\n",
    "                tmp = fuzhu[-1][j] + fuzhu[-2][j]\n",
    "                if board[-1][j] == 0 and tmp == 3:\n",
    "                    board[-1][j] = 1\n",
    "                elif board[-1][j] == 1:\n",
    "                    if tmp < 3 or tmp > 4:\n",
    "                        board[-1][j] = 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def gameOfLife(self, board: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify board in-place instead.\n",
    "        \"\"\"\n",
    "        if not board or not board[0]: pass\n",
    "        M, N = len(board), len(board[0])\n",
    "        def neighbour(i, j):\n",
    "            res = []\n",
    "            for l in range(i-1, i+2):\n",
    "                for r in range(j-1, j+2):\n",
    "                    if l >= 0 and l < M and r >= 0 and r < N and (l != i or j != r):\n",
    "                        res.append([l,r])\n",
    "            return res\n",
    "        for i in range(M):\n",
    "            for j in range(N):\n",
    "                for coor in neighbour(i,j):\n",
    "                    if board[coor[0]][coor[1]] % 2 == 1:\n",
    "                        board[i][j] += 2\n",
    "        for i in range(M):\n",
    "            for j in range(N):\n",
    "                if board[i][j] > 4 and board[i][j] < 8:\n",
    "                    board[i][j] = 1\n",
    "                else:\n",
    "                    board[i][j] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def gameOfLife(self, board: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify board in-place instead.\n",
    "        \"\"\"\n",
    "        def GetVal(board, i, j):\n",
    "            drect = [[-1, -1], [-1, 0], [-1, 1], [0, 1], [1, 1], [1, 0], [1, -1], [0, -1]]\n",
    "            n = 0\n",
    "            for d in drect:\n",
    "                if i + d[0] >= 0 and i + d[0] < len(board) and j + d[1] >= 0 and j + d[1] < len(board[0]):\n",
    "                    n += (board[i + d[0]][j + d[1]] % 2)\n",
    "            k = board[i][j]\n",
    "            if n == 3:\n",
    "                k = 1\n",
    "            elif n != 2:\n",
    "                k = 0\n",
    "            if k != board[i][j]:\n",
    "                if board[i][j] == 0:\n",
    "                    board[i][j] = 2\n",
    "                else:\n",
    "                    board[i][j] = 3\n",
    "        ret = []\n",
    "        for i in range(len(board)):\n",
    "            for j in range(len(board[i])):\n",
    "                GetVal(board, i, j)\n",
    "        for i in range(len(board)):\n",
    "            for j in range(len(board[i])):\n",
    "                if board[i][j] == 2:\n",
    "                    board[i][j] = 1\n",
    "                elif board[i][j] == 3:\n",
    "                    board[i][j] = 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def gameOfLife(self, board: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify board in-place instead.\n",
    "        \"\"\"\n",
    "        if not board or not board[0]:\n",
    "            return \n",
    "        m, n = len(board), len(board[0])\n",
    "        neibor = [[0,1],[0,-1],[1,0],[-1,0],[-1,-1],[-1,1],[1,-1],[1,1]]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                cnt = 0\n",
    "                for nei in neibor:\n",
    "                    if 0<= i+nei[0] < m and 0<= j+nei[1] < n:\n",
    "                        x = board[i+nei[0]][j+nei[1]]\n",
    "                        if x == 1 or x == -1:\n",
    "                            cnt+=1\n",
    "                if board[i][j] == 0 and cnt == 3:\n",
    "                    board[i][j] = -2\n",
    "                elif board[i][j] == 1 and (cnt < 2 or cnt > 3):\n",
    "                    board[i][j] = -1\n",
    "        \n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if board[i][j] == -1:\n",
    "                    board[i][j] = 0\n",
    "                elif board[i][j] == -2:\n",
    "                    board[i][j] = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def gameOfLife(self, board: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify board in-place instead.\n",
    "        \"\"\"\n",
    "        if not board or len(board) == 0:\n",
    "            return\n",
    "        m, n = len(board), len(board[0])\n",
    "\n",
    "        from functools import lru_cache\n",
    "        import copy\n",
    "\n",
    "        @lru_cache(None)\n",
    "        def get_neighbor(x, y):\n",
    "            return [(i, j) for i, j in (\n",
    "                (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1), (x + 1, y + 1), (x + 1, y - 1), (x - 1, y + 1),\n",
    "                (x - 1, y - 1)) if 0 <= i < m and 0 <= j < n]\n",
    "\n",
    "        board_bak = copy.deepcopy(board)\n",
    "        for i in range(0, m):\n",
    "            for j in range(0, n):\n",
    "                tmp = sum(1 for x, y in get_neighbor(i, j) if board_bak[x][y] == 1)\n",
    "                if tmp < 2 or tmp > 3:\n",
    "                    board[i][j] = 0\n",
    "                if tmp == 3:\n",
    "                    board[i][j] = 1\n",
    "\n",
    "        return \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def gameOfLife(self, board: List[List[int]]) -> None:\r\n",
    "        \"\"\"\r\n",
    "        Do not return anything, modify board in-place instead.\r\n",
    "        \"\"\"\r\n",
    "        # m = len(board)\r\n",
    "        # n = len(board[0])\r\n",
    "        # ans = []\r\n",
    "        # for i in range(m):\r\n",
    "        #     item = []\r\n",
    "        #     for j in range(n):\r\n",
    "        #         item.append(board[i][j])\r\n",
    "        #     ans.append(item)\r\n",
    "        \r\n",
    "        ans = copy.deepcopy(board)\r\n",
    "        dx = [-1, -1, -1, 0, 0, 1, 1, 1]\r\n",
    "        dy = [1, 0, -1, 1, -1, 1, 0, -1]\r\n",
    "        for i in range(len(board)):\r\n",
    "            for j in range(len(board[0])):\r\n",
    "                count = 0\r\n",
    "                for k in range(8):\r\n",
    "                    x = i + dx[k]\r\n",
    "                    y = j + dy[k]\r\n",
    "                    if (x >= 0 and x < len(board) and y >= 0 and y <len(board[0]) and board[x][y] == 1):\r\n",
    "                        count += 1\r\n",
    "                if count < 2 or count > 3:\r\n",
    "                    ans[i][j] = 0\r\n",
    "                elif count == 3:\r\n",
    "                    ans[i][j] = 1\r\n",
    "        board[:] = ans\r\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def gameOfLife(self, board: List[List[int]]) -> None:\n",
    "        direction = [\n",
    "            (-1, -1),\n",
    "            (-1, 0),\n",
    "            (-1, 1),\n",
    "            (0, -1),\n",
    "            (0, 1),\n",
    "            (1, -1),\n",
    "            (1, 0),\n",
    "            (1, 1),\n",
    "        ]\n",
    "        # 0 dead, 1 alive, 2 now alive next dead, 3 now dead next alive\n",
    "        m = len(board)\n",
    "        n = len(board[0])\n",
    "        for row in range(m):\n",
    "            for col in range(n):\n",
    "                alive_count = 0\n",
    "                for d in direction:\n",
    "                    i = row + d[0]\n",
    "                    j = col + d[1]\n",
    "                    if i >= 0 and i < m and j >= 0 and j < n:\n",
    "                        if board[i][j] == 1 or board[i][j] == 2:\n",
    "                            alive_count += 1\n",
    "                if board[row][col] == 1:\n",
    "                    if alive_count < 2 or alive_count > 3:\n",
    "                        board[row][col] = 2\n",
    "                else:\n",
    "                    if alive_count == 3:\n",
    "                        board[row][col] = 3\n",
    "\n",
    "        for row in range(m):\n",
    "            for col in range(n):\n",
    "                if board[row][col] == 2:\n",
    "                    board[row][col] = 0\n",
    "                elif board[row][col] == 3:\n",
    "                    board[row][col] = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def gameOfLife(self, board: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify board in-place instead.\n",
    "        \"\"\"\n",
    "        m, n = len(board), len(board[0])\n",
    "        def safe_get(i, j):\n",
    "            if i >= m or i < 0 or j >= n or j < 0:\n",
    "                return 0\n",
    "            return board[i][j]\n",
    "        t = ((-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1))\n",
    "        s = set()\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                a = sum([safe_get(i + di, j + dj) for (di, dj) in t])\n",
    "                if board[i][j] == 1 and (a < 2 or a > 3):\n",
    "                    s.add((i, j, 0))\n",
    "                elif board[i][j] == 0 and a == 3:\n",
    "                    s.add((i, j, 1))\n",
    "        for i, j, x in s:\n",
    "            board[i][j] = x\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def gameOfLife(self, board: List[List[int]]) -> None:\n",
    "        m = len(board)\n",
    "        n = len(board[0])\n",
    "        b1 = [[0] * n for _ in range(m)]\n",
    "        b2 = [[0] * (n + 2) for _ in range(m + 2)]\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                b2[i + 1][j + 1] = board[i][j]\n",
    "\n",
    "        for i in range(1, m + 1):\n",
    "            for j in range(1, n + 1):\n",
    "                x = (\n",
    "                    b2[i - 1][j - 1]\n",
    "                    + b2[i - 1][j]\n",
    "                    + b2[i - 1][j + 1]\n",
    "                    + b2[i][j - 1]\n",
    "                    + b2[i][j + 1]\n",
    "                    + b2[i + 1][j - 1]\n",
    "                    + b2[i + 1][j]\n",
    "                    + b2[i + 1][j + 1]\n",
    "                )\n",
    "                if b2[i][j] == 0:\n",
    "                    if x == 3:\n",
    "                        b1[i - 1][j - 1] = 1\n",
    "                else:\n",
    "                    if x == 2 or x == 3:\n",
    "                        b1[i - 1][j - 1] = 1\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                board[i][j]=b1[i][j]\n",
    "\n",
    "                \n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def gameOfLife(self, board: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify board in-place instead.\n",
    "        \"\"\"\n",
    "        n=len(board)\n",
    "        m=len(board[0])\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                cnt=0\n",
    "                for tx in range(-1,2):\n",
    "                    for ty in range(-1,2):\n",
    "                        if tx==0 and ty==0:\n",
    "                            continue\n",
    "                        xx,yy=i+tx,j+ty\n",
    "                        if (0<=xx<n and 0<=yy<m and (board[xx][yy]==1 or board[xx][yy]==2)):\n",
    "                            cnt+=1\n",
    "                if  board[i][j]==1 and (cnt<2 or cnt>3):\n",
    "                    board[i][j]=2\n",
    "                elif board[i][j]==0 and cnt==3:\n",
    "                    board[i][j]=3\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                if board[i][j]==2:\n",
    "                    board[i][j]=0\n",
    "                if board[i][j]==3:\n",
    "                    board[i][j]=1\n",
    "        return board\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def gameOfLife(self, board: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify board in-place instead.\n",
    "        \"\"\"\n",
    "        n = len(board)\n",
    "        m = len(board[0])\n",
    "        matrix = [[] for _ in range(n)]\n",
    "        for row in range(n):\n",
    "            for col in range(m):\n",
    "                current = board[row][col]\n",
    "                count = 0\n",
    "\n",
    "                left = col - 1\n",
    "                right = col + 1\n",
    "                top = row - 1\n",
    "                buttom = row + 1\n",
    "\n",
    "                if top >= 0:\n",
    "                    if left >= 0:\n",
    "                        if board[top][left] == 1:\n",
    "                            count += 1\n",
    "                    if board[top][col] == 1:\n",
    "                        count += 1\n",
    "                    if right < m:\n",
    "                        if board[top][right] == 1:\n",
    "                            count += 1\n",
    "                \n",
    "                if right < m:\n",
    "                    if board[row][right] == 1:\n",
    "                        count += 1\n",
    "                \n",
    "                if buttom < n:\n",
    "                    if right < m:\n",
    "                        if board[buttom][right] == 1:\n",
    "                            count += 1\n",
    "                    if board[buttom][col] == 1:\n",
    "                        count += 1\n",
    "                    if left >= 0:\n",
    "                        if board[buttom][left] == 1:\n",
    "                            count += 1\n",
    "                \n",
    "                if left >= 0:\n",
    "                    if board[row][left] == 1:\n",
    "                        count += 1\n",
    "                \n",
    "                if current == 1:\n",
    "                    if 2 <= count <= 3:\n",
    "                        matrix[row].append(1)\n",
    "                    else:\n",
    "                        matrix[row].append(0)\n",
    "                else:\n",
    "                    if count == 3:\n",
    "                        matrix[row].append(1)\n",
    "                    else:\n",
    "                        matrix[row].append(0)\n",
    "        \n",
    "        for row in range(n):\n",
    "            for col in range(m):\n",
    "                board[row][col] = matrix[row][col]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def gameOfLife(self, board: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify board in-place instead.\n",
    "        \"\"\"\n",
    "        m = len(board)\n",
    "        n = len(board[0])\n",
    "        import numpy\n",
    "        mid_board = numpy.zeros((m+2, n+2))\n",
    "        \n",
    "        for i, row in enumerate(board):\n",
    "            for j, col in enumerate(row):\n",
    "                if board[i][j] == 1:\n",
    "                    mid_board[i][j] += 1\n",
    "                    mid_board[i][j+1] += 1\n",
    "                    mid_board[i][j+2] += 1\n",
    "                    mid_board[i+1][j] += 1\n",
    "                    mid_board[i+1][j+2] += 1\n",
    "                    mid_board[i+2][j] += 1\n",
    "                    mid_board[i+2][j+1] += 1\n",
    "                    mid_board[i+2][j+2] += 1\n",
    "                    \n",
    "        #print(mid_board)           \n",
    "        \n",
    "        for i, row in enumerate(board):\n",
    "            for j, col in enumerate(row):\n",
    "                mid = mid_board[i+1][j+1]\n",
    "                \n",
    "                if board[i][j] == 1:\n",
    "                    if mid < 2:\n",
    "                        board[i][j] = 0\n",
    "                    elif mid > 3:\n",
    "                        board[i][j] = 0\n",
    "                elif mid == 3:\n",
    "                    board[i][j] = 1\n",
    "        \n",
    "                    \n",
    "                        \n",
    "    \n",
    "        \n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import numpy as np\n",
    "import copy\n",
    "class Solution:\n",
    "    def gameOfLife(self, board: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify board in-place instead.\n",
    "        \"\"\"\n",
    "        A=np.array(board)                        \n",
    "        row,col=A.shape                          \n",
    "        B= np.zeros((row+2,col+2))               \n",
    "        B[1:row+1,1:col+1]=A                     \n",
    "        C=np.zeros((row+2,col+2))                \n",
    "        print(B)                                 \n",
    "        for i in range(1,B.shape[0]-1):          \n",
    "            for j in range(1,B.shape[1]-1):      \n",
    "                sum=0                            \n",
    "                for k in range(-1,2):            \n",
    "                    for l in range(-1,2):        \n",
    "                        sum=sum+B[i+k][j+l]      \n",
    "                sum=sum-B[i][j]                  \n",
    "                if B[i][j]==1:                   \n",
    "                    if sum==2 or sum==3:         \n",
    "                        board[i-1][j-1] =1               \n",
    "                    else:                        \n",
    "                        board[i-1][j-1] =0               \n",
    "                else:                            \n",
    "                    if sum==3:                   \n",
    "                        board[i-1][j-1] =1               \n",
    "                    else:                        \n",
    "                        board[i-1][j-1] =0      \n",
    "        \n",
    "        #board=copy.deepcopy(C[1:row+1,1:col+1] )                \n",
    "        #print(\"board\",board)                                                 \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\r\n",
    "\r\n",
    "class Solution:\r\n",
    "    def gameOfLife(self, board: List[List[int]]) -> None:\r\n",
    "        \"\"\"\r\n",
    "        Do not return anything, modify board in-place instead.\r\n",
    "        \"\"\"\r\n",
    "        row = len(board)\r\n",
    "        col = len(board[0])\r\n",
    "        flag = [[True for _ in range(col)] for _ in range(row)]\r\n",
    "\r\n",
    "        def death(x, y):\r\n",
    "            num = 0\r\n",
    "            for i in range(max(0, x - 1), min(row, x+2)):\r\n",
    "                for j in range(max(0, y - 1), min(col, y + 2)):\r\n",
    "                    if board[i][j] == 1 and not (i == x and j == y):\r\n",
    "                        num += 1\r\n",
    "            if num < 2:\r\n",
    "                return 0\r\n",
    "            if num == 2 and board[x][y] == 1:\r\n",
    "                return 1\r\n",
    "            if num == 3:\r\n",
    "                return 1\r\n",
    "            return 0\r\n",
    "        for i in range(row):\r\n",
    "            for j in range(col):\r\n",
    "                flag[i][j] = board[i][j] == death(i, j)\r\n",
    "        for i in range(row):\r\n",
    "            for j in range(col):\r\n",
    "                if not flag[i][j]:\r\n",
    "                    if board[i][j] == 0:\r\n",
    "                        board[i][j] = 1\r\n",
    "                    else:\r\n",
    "                        board[i][j] = 0\r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "class Solution:\n",
    "    def gameOfLife(self, board: List[List[int]]) -> None:\n",
    "        neighbors = [(1,0), (1,-1), (0,-1), (-1,-1), (-1,0), (-1,1), (0,1), (1,1)]\n",
    "        rows = len(board)\n",
    "        cols = len(board[0])\n",
    "        # 遍历面板每一个格子里的细胞\n",
    "        for row in range(rows):\n",
    "            for col in range(cols):\n",
    "                # 对于每一个细胞统计其八个相邻位置里的活细胞数量\n",
    "                live_neighbors = 0\n",
    "                for neighbor in neighbors:\n",
    "                    # 相邻位置的坐标\n",
    "                    r = (row + neighbor[0])\n",
    "                    c = (col + neighbor[1])\n",
    "                    # 查看相邻的细胞是否是活细胞\n",
    "                    if (r < rows and r >= 0) and (c < cols and c >= 0) and abs(board[r][c]) == 1:\n",
    "                        live_neighbors += 1\n",
    "                # 规则 1 或规则 3 \n",
    "                if board[row][col] == 1 and (live_neighbors < 2 or live_neighbors > 3):\n",
    "                    # -1 代表这个细胞过去是活的现在死了\n",
    "                    board[row][col] = -1\n",
    "                # 规则 4\n",
    "                if board[row][col] == 0 and live_neighbors == 3:\n",
    "                    # 2 代表这个细胞过去是死的现在活了\n",
    "                    board[row][col] = 2\n",
    "        # 遍历 board 得到一次更新后的状态\n",
    "        for row in range(rows):\n",
    "            for col in range(cols):\n",
    "                if board[row][col] > 0:\n",
    "                    board[row][col] = 1\n",
    "                else:\n",
    "                    board[row][col] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def gameOfLife(self, board: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify board in-place instead.\n",
    "        \"\"\"\n",
    "        import numpy as np \n",
    "        r,c = len(board),len(board[0])\n",
    "        board_exp = np.array([[0 for _ in range(c + 2)] for _ in range( r + 2)])\n",
    "        board_exp[1:1 +r, 1:1 + c]=np.array(board)\n",
    "        kernel = np.array([[1,1,1],[1,0,1],[1,1,1]])\n",
    "        for i in range(1,r + 1):\n",
    "            for j in range(1, c + 1):\n",
    "                  temp_sum = np.sum(kernel * board_exp[i - 1:i + 2, j - 1:j +2])\n",
    "                  if board_exp[i, j] == 1:\n",
    "                    if temp_sum < 2 or temp_sum >3:\n",
    "                        board[ i - 1][j - 1] = 0\n",
    "                  else:               \n",
    "                    if temp_sum == 3:\n",
    "                        board[i -1][j -1] = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def gameOfLife(self, board: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify board in-place instead.\n",
    "        \"\"\"\n",
    "        import numpy as np\n",
    "        r, c = len(board), len(board[0])\n",
    "        # 下面两行做 zero padding\n",
    "        board_exp = np.array([[0 for _ in range(c + 2)] for _ in range(r + 2)])\n",
    "        board_exp[1:1 + r, 1:1 + c] = np.array(board)\n",
    "        # 设置卷积核\n",
    "        kernel = np.array([[1, 1, 1], [1, 0, 1], [1, 1, 1]])\n",
    "        # 开始卷积\n",
    "        for i in range(1, r + 1):\n",
    "            for j in range(1, c + 1):\n",
    "                # 统计细胞周围 8 个位置的状态\n",
    "                temp_sum = np.sum(kernel * board_exp[i - 1:i + 2, j - 1:j + 2])\n",
    "                # 按照题目规则进行判断\n",
    "                if board_exp[i, j] == 1:\n",
    "                    if temp_sum < 2 or temp_sum > 3:\n",
    "                        board[i - 1][j - 1] = 0\n",
    "                else:\n",
    "                    if temp_sum == 3:\n",
    "                        board[i - 1][j - 1] = 1  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def gameOfLife(self, board: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify board in-place instead.\n",
    "        \"\"\"\n",
    "        rows = len(board)\n",
    "        if board == 0:\n",
    "            return \n",
    "        cols = len(board[0])\n",
    "        if cols == 0:\n",
    "            return \n",
    "        import numpy as np\n",
    "        cells = np.zeros((rows+2, cols+2), dtype=np.int32)\n",
    "        cells[1:-1, 1:-1] = np.array(board)\n",
    "        for i in np.arange(1, rows+1):\n",
    "            for j in np.arange(1, cols+1):\n",
    "                count = cells[i-1:i+2, j-1:j+2].sum() - cells[i, j]\n",
    "                if count < 2 and cells[i, j] == 1:\n",
    "                    board[i-1][j-1] = 0\n",
    "                elif count in (2, 3) and cells[i, j] == 1:\n",
    "                    board[i-1][j-1] = 1\n",
    "                elif count > 3 and cells[i, j] == 1:\n",
    "                    board[i-1][j-1] = 0\n",
    "                elif count == 3 and cells[i, j] == 0:\n",
    "                    board[i-1][j-1] = 1\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def gameOfLife(self, board: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify board in-place instead.\n",
    "        \"\"\"\n",
    "        import numpy as np\n",
    "        r,c = len(board),len(board[0])\n",
    "        board_exp=np.array([[0 for _ in range(c+2)] for _ in range(r+2)])\n",
    "        board_exp[1:1+r,1:1+c] =np.array(board)\n",
    "        kernel = np.array([[1,1,1],[1,0,1],[1,1,1]])\n",
    "\n",
    "        for i in range(1,r+1):\n",
    "            for j in range(1,c+1):\n",
    "                temp_sum=np.sum(kernel*board_exp[i-1:i+2,j-1:j+2])\n",
    "                if board_exp[i,j]==1:\n",
    "                    if temp_sum<2 or temp_sum>3:\n",
    "                        board[i-1][j-1]=0\n",
    "                else:\n",
    "                    if temp_sum==3:\n",
    "                        board[i-1][j-1]=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def gameOfLife(self, board: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify board in-place instead.\n",
    "        \"\"\"\n",
    "        import numpy as np\n",
    "        r, c = len(board), len(board[0])\n",
    "        # 下面两行做 zero padding\n",
    "        board_exp = np.array([[0 for _ in range(c + 2)] for _ in range(r + 2)])\n",
    "        board_exp[1:1 + r, 1:1 + c] = np.array(board)\n",
    "        # 设置卷积核\n",
    "        kernel = np.array([[1, 1, 1], [1, 0, 1], [1, 1, 1]])\n",
    "        # 开始卷积\n",
    "        for i in range(1, r + 1):\n",
    "            for j in range(1, c + 1):\n",
    "                # 统计细胞周围 8 个位置的状态\n",
    "                temp_sum = np.sum(kernel * board_exp[i - 1:i + 2, j - 1:j + 2])\n",
    "                # 按照题目规则进行判断\n",
    "                if board_exp[i, j] == 1:\n",
    "                    if temp_sum < 2 or temp_sum > 3:\n",
    "                        board[i - 1][j - 1] = 0\n",
    "                else:\n",
    "                    if temp_sum == 3:\n",
    "                        board[i - 1][j - 1] = 1 \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def gameOfLife(self, board: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify board in-place instead.\n",
    "        \"\"\"\n",
    "        import numpy as np\n",
    "        r, c = len(board), len(board[0])\n",
    "        # 下面两行做 zero padding\n",
    "        board_exp = np.array([[0 for _ in range(c + 2)] for _ in range(r + 2)])\n",
    "        board_exp[1:1 + r, 1:1 + c] = np.array(board)\n",
    "        # 设置卷积核\n",
    "        kernel = np.array([[1, 1, 1], [1, 0, 1], [1, 1, 1]])\n",
    "        # 开始卷积\n",
    "        for i in range(1, r + 1):\n",
    "            for j in range(1, c + 1):\n",
    "                # 统计细胞周围 8 个位置的状态\n",
    "                temp_sum = np.sum(kernel * board_exp[i - 1:i + 2, j - 1:j + 2])\n",
    "                # 按照题目规则进行判断\n",
    "                if board_exp[i, j] == 1:\n",
    "                    if temp_sum < 2 or temp_sum > 3:\n",
    "                        board[i - 1][j - 1] = 0\n",
    "                else:\n",
    "                    if temp_sum == 3:\n",
    "                        board[i - 1][j - 1] = 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def gameOfLife(self, board: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify board in-place instead.\n",
    "        \"\"\"\n",
    "        import numpy as np\n",
    "        r, c = len(board), len(board[0])\n",
    "        # 下面两行做 zero padding\n",
    "        board_exp = np.array([[0 for _ in range(c + 2)] for _ in range(r + 2)])\n",
    "        board_exp[1:1 + r, 1:1 + c] = np.array(board)\n",
    "        # 设置卷积核\n",
    "        kernel = np.array([[1, 1, 1], [1, 0, 1], [1, 1, 1]])\n",
    "        # 开始卷积\n",
    "        for i in range(1, r + 1):\n",
    "            for j in range(1, c + 1):\n",
    "                # 统计细胞周围 8 个位置的状态\n",
    "                temp_sum = np.sum(kernel * board_exp[i - 1:i + 2, j - 1:j + 2])\n",
    "                # 按照题目规则进行判断\n",
    "                if board_exp[i, j] == 1:\n",
    "                    if temp_sum < 2 or temp_sum > 3:\n",
    "                        board[i - 1][j - 1] = 0\n",
    "                else:\n",
    "                    if temp_sum == 3:\n",
    "                        board[i - 1][j - 1] = 1 \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import copy\n",
    "class Solution:\n",
    "    def gameOfLife(self, board: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify board in-place instead.\n",
    "        \"\"\"\n",
    "        import numpy as np\n",
    "        r,c = len(board),len(board[0])\n",
    "        board_exp = np.array([[0 for _ in range(c + 2)] for _ in range(r + 2)])\n",
    "        board_exp[1:1+r,1:1+c] = np.array(board)\n",
    "        kernel = np.array([[1,1,1],[1,0,1],[1,1,1]])\n",
    "        for i in range(1,r+1):\n",
    "            for j in range(1,c + 1):\n",
    "                temp_sum = np.sum(kernel * board_exp[i-1:i+2,j-1:j+2])\n",
    "                if board_exp[i,j] == 1:\n",
    "                    if temp_sum < 2 or temp_sum > 3:\n",
    "                        board[i-1][j-1]=  0\n",
    "                else:\n",
    "                    if temp_sum == 3:\n",
    "                        board[i - 1][j - 1] = 1  \n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
